package com.wan37.auth.utils;

import com.wan37.auth.domain.User;
import com.wan37.auth.handler.AuthServerHandler;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.auth.Auth;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;

/**
 * @author : luoyong
 * @date : 2020-05-27 17:21
 **/
public final class RouteUtils {

    private RouteUtils() {
    }

    /**
     * 向客户端回复消息.
     *
     * @param code
     * @param content
     * @param netId
     * @param userId
     */
    public static void sendResponse(int code, String content, long netId, String userId) {

        Auth.SResponse.Builder sb = Auth.SResponse.newBuilder();
        sb.setCode(code);
        sb.setContent(content);
        Auth.SResponse sResponse = sb.build();

        //sResponse消息
        ByteBuf sendBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SRESPONSE, sResponse);

        //通过gateServer转发给客户端.
        AuthServerHandler.getGateConnCtx().writeAndFlush(sendBuf);
    }

    /**
     * 向客户端回复消息.
     *
     * @param code
     * @param content
     * @param netId
     * @param userId
     */
    public static void sendResponse(int code, String content, long netId, String userId, User user) {

        Auth.SResponse.Builder sb = Auth.SResponse.newBuilder();
        sb.setCode(code);
        sb.setContent(content);
        GameEntity.User.Builder ub = GameEntity.User.newBuilder();
        ub.setUserId(user.getUserId());
        ub.setName(user.getNickname());
        ub.setGold(user.getGold());
        ub.setIngot(user.getIngot());
        sb.setUser(ub.build());
        Auth.SResponse sResponse = sb.build();

        //sResponse消息
        ByteBuf sendBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SRESPONSE, sResponse);

        //通过gateServer转发给客户端.
        AuthServerHandler.getGateConnCtx().writeAndFlush(sendBuf);
    }
}
